package com.woniu.secondhand.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @Title
 * @Author 武术有栖
 * @Description:订单超时队列和交换机
 */
@Configuration
public class OrderQueueConfig {
    public final static String ORDER_EXCHANGE = "order_exchange";
    public final static String DLQ_ORDER_EXCHANGE = "dlq_order_exchange";
    public final static String ORDER_QUEUE = "order_queue";
    public final static String DLQ_ORDER_QUEUE = "dlq_order_queue";
    public final static String ROUTING_KEY = "order_timeout";
    public final static Long X_MESSAGE_TTL = 15 * 1000L;

    public final static String ORDER_EXCHANGE1 = "order_exchange1";
    public final static String DLQ_ORDER_EXCHANGE1 = "dlq_order_exchange1";
    public final static String ORDER_QUEUE1 = "order_queue1";
    public final static String DLQ_ORDER_QUEUE1 = "dlq_order_queue1";
    public final static String ROUTING_KEY1 = "order_timeout1";
    public final static Long X_MESSAGE_TTL1 = 15 * 1000L;

    @Bean
    public Exchange orderExchange() {
        return ExchangeBuilder.directExchange(ORDER_EXCHANGE).durable(true).build();
    }

    @Bean
    public Exchange dlqOrderExchange() {
        return ExchangeBuilder.directExchange(DLQ_ORDER_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue orderQueue() {
        // 配置队列的相关参数
        Map<String, Object> params = new HashMap<>();
        // 设置消息的过期时间，时间为毫秒
        params.put("x-message-ttl", X_MESSAGE_TTL);
        // 配置死信交换机
        params.put("x-dead-letter-exchange", DLQ_ORDER_EXCHANGE);
        // 配置死信路由key
        params.put("x-dead-letter-routing-key", ROUTING_KEY);
        return QueueBuilder.durable(ORDER_QUEUE).withArguments(params).build();
    }

    @Bean
    public Queue dlqOrderQueue() {
        return QueueBuilder.durable(DLQ_ORDER_QUEUE).build();
    }

    @Bean
    public Binding orderBinding(@Qualifier("orderQueue") Queue queue, @Qualifier("orderExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY).noargs();
    }

    @Bean
    public Binding dlqOrderBinding(@Qualifier("dlqOrderQueue") Queue queue, @Qualifier("dlqOrderExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY).noargs();
    }


    @Bean
    public Exchange orderExchange1() {
        return ExchangeBuilder.directExchange(ORDER_EXCHANGE1).durable(true).build();
    }

    @Bean
    public Exchange dlqOrderExchange1() {
        return ExchangeBuilder.directExchange(DLQ_ORDER_EXCHANGE1).durable(true).build();
    }

    @Bean
    public Queue orderQueue1() {
        // 配置队列的相关参数
        Map<String, Object> params = new HashMap<>();
        // 设置消息的过期时间，时间为毫秒
        params.put("x-message-ttl", X_MESSAGE_TTL1);
        // 配置死信交换机
        params.put("x-dead-letter-exchange", DLQ_ORDER_EXCHANGE1);
        // 配置死信路由key
        params.put("x-dead-letter-routing-key", ROUTING_KEY1);
        return QueueBuilder.durable(ORDER_QUEUE1).withArguments(params).build();
    }

    @Bean
    public Queue dlqOrderQueue1() {
        return QueueBuilder.durable(DLQ_ORDER_QUEUE1).build();
    }

    @Bean
    public Binding orderBinding1(@Qualifier("orderQueue1") Queue queue, @Qualifier("orderExchange1") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY1).noargs();
    }

    @Bean
    public Binding dlqOrderBinding1(@Qualifier("dlqOrderQueue1") Queue queue, @Qualifier("dlqOrderExchange1") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY1).noargs();
    }
}
